import request from '@/utils/request'
var formMixin = {
  data() {
    return {
      confirmLoading: false,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "Edit",
        create: "Create"
      },
      tipMap: {
        update: "修改成功",
        create: "新增成功"
      },
      Urls: {
        addUrl: '',
        editUrl: '',
        getByIdUrl: '',
        uploadFileUrl: '',
        dictTypeUrl: '/api/dict/typeList'
      },
      form: {
        examQusList: []
      },
      rules: {
        must: [{
          required: true,
          message: "此字段为必填",
          trigger: "blur"
        }],
      },
      pickerOptionsStart: {
        disabledDate: time => {
          let endDateVal = this.form.endTime;
          if (endDateVal) {
            return time.getTime() > new Date(endDateVal).getTime();
          }
        }
      },
      pickerOptionsEnd: {
        disabledDate: time => {
          let beginDateVal = this.form.startTime;
          if (beginDateVal) {
            return (
              time.getTime() <
              new Date(beginDateVal).getTime() - 1 * 24 * 60 * 60 * 1000
            )
          }
        }
      },
      fileList: []
    }
  },
  filters: {},
  mounted() {},
  methods: {
    resetForm() {
      this.form = {}
    },
    add() {
      this.resetForm()
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
      this.beforeOpen()
    },
    edit(row) {
      this.beforeOpen(row)
      this.setForm(row)
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });

    },
    // 打开之前
    beforeOpen(row) {},
    // 请求详情
    setForm(row) {
      this.confirmLoading = true
      request({
          url: this.Urls.getByIdUrl + row.id,
          method: "get"
        })
        .then(res => {
          this.confirmLoading = false
          if (res.code == 0) {
            this.form = Object.assign({}, res.data);
            this.fillForm(res.data)
          } else {
            that.$notify({
              title: "提示",
              message: res.msg,
              type: "error",
              duration: 2000
            });
          }
        })
        .catch(err => {
          this.confirmLoading = false
        });
    },
    // 请求数据格式化
    fillForm(data) {},
    // 提交数据格式化
    formatData(form) {
      return form
    },
    submitForm() {
      let that = this
      let url = {
        update: that.Urls.editUrl + that.form.id,
        create: that.Urls.addUrl
      }
      console.log('form', that.form)
      that.$refs["dataForm"].validate(valid => {
        if (valid) {
          let formData = that.formatData(that.form)
          this.confirmLoading = true
          request({
            url: url[that.dialogStatus],
            method: 'post',
            data: formData
          }).then((res) => {
            console.log('res form', res)
            this.confirmLoading = false
            if (res.code == 0) {
              that.$notify({
                title: '提示',
                message: that.tipMap[that.dialogStatus],
                type: 'success',
                duration: 2000
              })
              that.dialogFormVisible = false
              that.$parent.afterSubmit()
            } else {
              that.$notify({
                title: '提示',
                message: res.msg,
                type: 'error',
                duration: 2000
              })
            }
          }).catch((err) => {
            this.confirmLoading = false
          })
        }
      });
    },
    // 字典
    getDictList(type, data) {
      request({
        url: this.Urls.dictTypeUrl,
        method: 'get',
        params: {
          type: type
        }
      }).then(res => {
        if (res.code == 0) {
          this[data] = res.data.records
        } else {
          that.$notify({
            title: '提示',
            message: res.msg,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
  }
}
export default formMixin
